define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'clue/clue/index',
                    add_url: 'clue/clue/add',
                    edit_url: 'clue/clue/edit',
                    del_url: 'clue/clue/del',
                    multi_url: 'clue/clue/multi',
                    table: 'clue',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                searchFormVisible: true,
                maintainSelected: 1,
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: '线索编号',},
                        {field: 'useUnitName', title: '业主',},
                        {field: 'useUnitPhone', title: '业主电话',},
                        {field: 'address', title: '地区',},
                        {field: 'liftName', title: '梯位信息',},
                        {field: 'no', title: '电梯编号',},
                        {
                            field: 'status', title: __('线索状态'),
                            searchList: {
                                "1": __('待沟通'),
                                "2": __('沟通中'),
                                "3": __('已流转'),
                            },
                            formatter: function (value) {
                                switch (value) {
                                    case 1:
                                        value = '待沟通';
                                        break;
                                    case 2:
                                        value = '沟通中';
                                        break;
                                    case 3:
                                        value = '已流转';
                                        break;

                                }
                                return value;
                            }
                        },
                        {field: 'saleman.real_name', title: '业务员', operate: false},
                        {
                            field: 'salesman_id', title: '业务员', addclass: 'selectpage',
                            extend: 'data-source="check/free_clean/getSaleMan" data-field="title"', visible: false
                        },

                        {field: 'type', title: '最近备注', operate: false},
                        {
                            field: 'last_time',
                            title: __('最近跟进时间'),
                            formatter: Table.api.formatter.datetime,
                            operate: 'RANGE',
                            addclass: 'datetimerange',
                            sortable: true
                        },
                        {
                            field: 'assign_time',
                            title: __('指派时间'),
                            formatter: Table.api.formatter.datetime,
                            operate: 'RANGE',
                            addclass: 'datetimerange',
                            sortable: true
                        },
                        {
                            field: 'create_time',
                            title: __('添加时间'),
                            formatter: Table.api.formatter.datetime,
                            operate: 'RANGE',
                            addclass: 'datetimerange',
                            sortable: true
                        },
                        {
                            field: 'green_map',
                            title: __('绿标'),
                            operate: false,
                            events: Table.api.events.image,
                            formatter: Table.api.formatter.image
                        },
                        {
                            field: 'operate', title: __('Operate'), table: table, buttons: [
                                {
                                    name: 'detail',
                                    text: __('查看详情'),
                                    title: __('查看详情'),
                                    classname: 'btn btn-xs btn-success btn-detail btn-magic btn-dialog',
                                    icon: 'fa fa-eye',
                                    url: 'clue/clue/detail?id={id}',
                                    // extend:'data-area=["60%","70%"]',
                                    callback: function (data) {
                                    },
                                },
                                {
                                    name: 'assign',
                                    text: __('指派业务员'),
                                    title: __('指派业务员'),
                                    classname: 'btn btn-xs btn-danger btn-assign btn-magic btn-dialog',
                                    icon: 'fa fa-paint-brush',
                                    url: 'clue/clue/do_assign?id={id}',
                                    // extend:'data-area=["60%","70%"]',
                                    callback: function (data) {
                                    },
                                },
                            ], events: Table.api.events.operate, formatter: function (value, row, index) {
                                var that = $.extend({}, this);
                                $(table).data("operate-edit", true); // 列表页面隐藏 .编辑operate-edit  - 删除按钮operate-del
                                $(table).data("operate-del", null); // 列表页面隐藏 .编辑operate-edit  - 删除按钮operate-del
                                that.table = table;
                                return Table.api.formatter.operate.call(that, value, row, index);
                            }
                        },
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        detail: function () {
            Controller.api.bindevent();
        },
        //指派业务员
        do_assign: function () {
            Controller.api.bindevent();
            var is_have_saleman = Config.is_have_saleman;
            if (is_have_saleman == 1) {
                $('.saleman').show();
            } else {
                $('.saleman').hide();
            }
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});
